﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Umbraco.Framework.Diagnostics;
using PBDesk.Utilities;
using System.Reflection;
using System.Diagnostics;

namespace PBDesk.com.Extension.UmbHelper
{
    public class MyLog
    {
        public static bool IsCustomLogEnabled()
        {
            bool customLogEnabled = false;
            if (bool.TryParse(ConfigurationManager.AppSettings["CustomLogEnabled"] ?? "false", out customLogEnabled))
                return customLogEnabled;
            else
                return false;

        }

        public static void WriteFormat<T>(string errMsg, LogType logType = LogType.InfoTrace, params object[] parameters)
        {
            if (IsCustomLogEnabled())
            {
                Write<T>(string.Format(errMsg, parameters), logType);
            }

        }
        public static void Write<T>(string errMsg, LogType logType = LogType.InfoTrace)
        {
            if (IsCustomLogEnabled() && !errMsg.IsNullOrEmpty())
            {
                WriteLog<T>(errMsg, logType);                              
            }
        }
        public static void Write<T>(string msg1, string msg2, LogType logType = LogType.InfoTrace)
        {
            if (IsCustomLogEnabled() && !msg1.IsNullOrEmpty())
            {
                WriteFormat<T>("{0} :: {1}", logType, msg1, msg2 ?? string.Empty);
            }
        }

        private static void WriteLog<T>(string errMsg, LogType logType = LogType.InfoTrace)
        {
            switch (logType)
            {
                case LogType.Warn:
                    {
                        LogHelper.Warn<T>(errMsg);
                        break;
                    }
                case LogType.InfoTrace:
                    {
                        LogHelper.TraceIfEnabled<T>(() => { return errMsg; });
                        break;
                    }
            }
        }

        public static void WriteErr<T>(string errMsg, Exception ex)
        {
            LogHelper.Error<T>(errMsg, ex);
        }
        
}

    public enum LogType
    {
        InfoTrace,
        Warn,
    }

  
}
